//
//  ASubmitOrderFormViewController.m
//  DistributorAB
//
//  Created by 主事丫环 on 2019/9/27.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "ASubmitOrderFormViewController.h"
#import "ASubmitOrderFormTableViewCell.h"

@interface ASubmitOrderFormViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *mytableView;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NSMutableArray *mydateSource;
@property (nonatomic, strong) UIView *tabFootView;

@property (nonatomic, assign) BOOL rightChose;
@property (nonatomic, assign) BOOL secChose;
@property (nonatomic, assign) BOOL rowChose;
@property (nonatomic, assign) BOOL allChose;

@property (nonatomic, strong) UIButton *secSelBtn; //分区区头选择按钮
@property (nonatomic, strong) UIView *bgViewsecCancle; //取消原因 弹窗
@property (nonatomic, strong) UIView *bgViewsecConfirm; //修改数量 弹窗

@end

@implementation ASubmitOrderFormViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"预订单";
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
    
    self.mydateSource = [NSMutableArray arrayWithCapacity:1];
    
    //    [self setupRightItem];
    [self showdetaile];
    [self shwoBgviewsecCancle]; //取消原因
    [self shwoBgviewsecConfirm]; //修改数量
    // Do any additional setup after loading the view.
}

-(void)showdetaile {
    
    UIView *bgview = [[UIView alloc]initWithFrame:CGRectMake(0,kNaviHeight, KWIDTH, 120)];
    [self.scrollView addSubview:bgview];
    [self.view addSubview:self.scrollView];
    bgview.backgroundColor = [UIColor whiteColor];
    
    UILabel *dateTitLab = [[UILabel alloc] init];
    //dateTitLab.backgroundColor = [UIColor redColor];
    dateTitLab.frame = CGRectMake(16, 11, 70, 20);
    dateTitLab.text = @"下单时间";
    dateTitLab.font = FontSize(14);
    dateTitLab.textColor = K666666;
    [bgview addSubview:dateTitLab];
    
    UILabel *dateConLab = [[UILabel alloc] init];
    //dateConLab.backgroundColor = [UIColor cyanColor];
    dateConLab.frame = CGRectMake(dateTitLab.right +23, 11, KWIDTH-32-70-20, 20);
    dateConLab.text = @"2019-09-25 09:04:50";
    dateConLab.font = FontSize(12);
    dateTitLab.textColor = K999999;
    [bgview addSubview:dateConLab];
    
    UILabel *line = [[UILabel alloc] init];
    line.backgroundColor = [UIColor colorWithHexString:@"#DBDBDB"];
    line.frame = CGRectMake(10, dateTitLab.bottom +8, KWIDTH -20, 1);
    [bgview addSubview:line];
    
    UILabel *nameTitLab = [[UILabel alloc] init];
    //nameTitLab.backgroundColor = [UIColor cyanColor];
    nameTitLab.frame = CGRectMake(16, line.bottom +9, 70, 20);
    nameTitLab.text = @"";
    nameTitLab.font = FontSize(12);
    nameTitLab.textColor = K666666;
    [bgview addSubview:nameTitLab];
    
    UILabel *nameConLab = [[UILabel alloc] init];
    //nameConLab.backgroundColor = [UIColor orangeColor];
    nameConLab.frame = CGRectMake(nameTitLab.right +23, line.bottom +9, (KWIDTH -32-70-20)/2, 20);
    nameConLab.text = @"海燕";
    nameConLab.font = FontSize(14);
    nameConLab.textColor = [UIColor colorWithHexString:@"#272727"];
    [bgview addSubview:nameConLab];
    
    UILabel *phoneLab = [[UILabel alloc] init];
    //phoneLab.backgroundColor = [UIColor greenColor];
    phoneLab.frame = CGRectMake(nameConLab.right, line.bottom +9, (KWIDTH -32-70-20)/2, 20);
    phoneLab.text = @"15958961147";
    phoneLab.textAlignment = NSTextAlignmentRight;
    phoneLab.font = FontSize(14);
    phoneLab.textColor = [UIColor colorWithHexString:@"#272727"];
    [bgview addSubview:phoneLab];
    
    UILabel *addTitLab = [[UILabel alloc] init];
    //addTitLab.backgroundColor = [UIColor orangeColor];
    addTitLab.frame = CGRectMake(16, nameTitLab.bottom +9, 70, 20);
    addTitLab.text = @"收货地址";
    addTitLab.font = FontSize(14);
    addTitLab.textColor = K666666;
    [bgview addSubview:addTitLab];
    
    UILabel *addConLab = [[UILabel alloc] init];
    //addConLab.backgroundColor = [UIColor orangeColor];
    addConLab.frame = CGRectMake(addTitLab.right +23, nameConLab.bottom +5, KWIDTH-32-70-20, 40);
    addConLab.text = @"河南省郑州市管城回族区航海路大学路升龙国际城机会几单元3号341";
    addConLab.numberOfLines = 0;
    addConLab.font = FontSize(12);
    addConLab.textColor = K666666;
    [bgview addSubview:addConLab];
    
    self.tabFootView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, 56)];
    
    _mytableView = [[UITableView alloc] init];
    _mytableView.frame = CGRectMake(0, bgview.bottom +2, KWIDTH, KHEIGHT-kNaviHeight-kTabbarHeight-56);
    _mytableView.delegate = self;
    _mytableView.dataSource = self;
    _mytableView.estimatedRowHeight = 90;
    _mytableView.rowHeight = UITableViewAutomaticDimension;
    _mytableView.tableFooterView = [UIView new];
    [_mytableView registerClass:[ASubmitOrderFormTableViewCell class] forCellReuseIdentifier:@"ASubmitOrderFormTableViewCell"];
    _mytableView.backgroundColor = [UIColor clearColor];
    adjustInset(_mytableView);
    [_mytableView reloadData];
    _mytableView.tableFooterView = self.tabFootView;
    [_scrollView addSubview:_mytableView];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //return _mydateSource.count;
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 300;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc] init];
    headerView.backgroundColor = [UIColor whiteColor];
    headerView.frame = CGRectMake(20, 0, KWIDTH-40, 40);
    
    UIButton *secSelBtn = [[UIButton alloc] init];
    //secSelBtn.backgroundColor = [UIColor redColor];
    secSelBtn.frame = CGRectMake(16, 10, 20, 20);
    [secSelBtn setImage:[UIImage imageNamed:@"dianpu (1)"] forState:UIControlStateNormal];
    [secSelBtn addTarget:self action:@selector(secSelBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:secSelBtn];
    self.secSelBtn = secSelBtn;
    
    UILabel *shopTitLab = [[UILabel alloc] init];
    //shopTitLab.backgroundColor = [UIColor yellowColor];
    shopTitLab.frame = CGRectMake(secSelBtn.right +2, 10, KWIDTH-20-6-30-20-10, 20);
    shopTitLab.text = @"格力配件店铺";
    shopTitLab.numberOfLines = 0;
    shopTitLab.font = FontSize(14);
    shopTitLab.textColor = [UIColor colorWithHexString:@"#70BE68"];
    [headerView addSubview:shopTitLab];
    //self.shopTitLab = shopTitLab;
    
    UIImageView *topBgImg = [[UIImageView alloc] init];
    //topBgImg.backgroundColor = [UIColor orangeColor];
    topBgImg.frame = CGRectMake(shopTitLab.right +3, 10, 20, 20);
    [topBgImg setImage:imgname(@"jinruimage")];
    [headerView addSubview:topBgImg];
    
    return headerView;
}

//分区区头 选择按钮 点击事件处理
- (void)secSelBtnAction:(UIButton *)button {
    
    NSLog(@"您点击了 分区 区头 的选择按钮");
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    //定义Cell标识 每一个Cell对应一个自己的标识
    NSString *CellIdentitier = [NSString stringWithFormat:@"cell%ld%ld",indexPath.section, indexPath.row];
    
    //通过唯一标识创建Cell实例
    ASubmitOrderFormTableViewCell *mycell = [tableView dequeueReusableCellWithIdentifier:CellIdentitier];
    //判断为空进行初始化  --(当拉动页面显示超过主页面内容的时候就会重用之前的cell,w而不会再次初始化)
    if (!mycell) {
        mycell = [[ASubmitOrderFormTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentitier];
    }
    self.mytableView.separatorStyle = UITableViewCellEditingStyleNone;//不显示分割线
    
    mycell.myblock = ^(NSUInteger ind, NSString * _Nonnull str) {
        switch (ind) {
            case 0: {
                //输入名字
                //model.name = str;
                //NSLog(@"%@", str);
                
                self.rowChose =! self.rowChose;
                if (self.rowChose) {
                    [mycell.rowSelBtn setImage:[UIImage imageNamed:@"selequyu"] forState:UIControlStateNormal];
                } else {
                    [mycell.rowSelBtn setImage:[UIImage imageNamed:@"seleQuyuyew"] forState:UIControlStateNormal];
                }
            }
                break;
            case 1: {
                //修改数量 弹窗
                [[UIApplication sharedApplication].keyWindow addSubview:self.bgViewsecConfirm];
                
            }
                break;
            case 2: {
                //取消原因 弹窗
                [[UIApplication sharedApplication].keyWindow addSubview:self.bgViewsecCancle];
                
            }
                break;
            default:
                break;
        }
    };
    
    return mycell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

/**
 弹出框的背景图 取消原因
 */
-(void)shwoBgviewsecCancle{
    self.bgViewsecCancle = [[UIView alloc]init];
    self.bgViewsecCancle.frame = self.view.bounds;
    self.bgViewsecCancle.backgroundColor = RGBA(1, 1, 1, 0.5);
    UIView *whiteBGView = [[UIView alloc]init];
    whiteBGView.backgroundColor = [UIColor whiteColor];
    whiteBGView.layer.masksToBounds = YES;
    whiteBGView.layer.cornerRadius = 6;
    [self.bgViewsecCancle addSubview:whiteBGView];
    [whiteBGView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.height.offset(240);
        make.left.offset(22);
        make.right.offset(-22);
    }];
    
    UILabel *UpLable = [[UILabel alloc]initWithFrame:CGRectMake(38, 48, KWIDTH-44-76, 21)];
    [whiteBGView addSubview:UpLable];
    UpLable.font = FontSize(16);
    UpLable.textColor = [UIColor colorWithHexString:@"#262626"];
    UpLable.textAlignment =  NSTextAlignmentLeft;
    UpLable.text = @"取消原因:";
    
    UITextField *cancelTF = [[UITextField alloc] init];
    //cancelTF.backgroundColor = [UIColor orangeColor];
    cancelTF.frame = CGRectMake(38, UpLable.bottom +15, KWIDTH-44-76, 39);
    cancelTF.placeholder = @"请选择取消原因";
    cancelTF.textColor = K333333;
    cancelTF.font = FontSize(14);
    cancelTF.layer.cornerRadius = 5;
    cancelTF.layer.borderColor = [UIColor colorWithHexString:@"#BFBFBF"].CGColor;
    cancelTF.layer.borderWidth = 1;
    [whiteBGView addSubview:cancelTF];
    
    UIButton *cancelButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [cancelButton setTitle:@"暂不取消" forState:(UIControlStateNormal)];
    [cancelButton setTitleColor:[UIColor colorWithHexString:@"#70BE68"] forState:(UIControlStateNormal)];
    [cancelButton setBackgroundColor:[UIColor whiteColor]];
    cancelButton.layer.masksToBounds = YES;
    cancelButton.layer.cornerRadius = 4;
    cancelButton.layer.borderWidth = 1;
    cancelButton.layer.borderColor = [UIColor colorWithHexString:@"#70BE68"].CGColor;
    cancelButton.titleLabel.font = FontSize(14);
    [cancelButton addTarget:self action:@selector(cancelButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [whiteBGView addSubview:cancelButton];
    [cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(36);
        make.left.offset(28);
        make.right.equalTo(whiteBGView.mas_centerX).offset(-13);
        make.bottom.offset(-19);
    }];
    
    UIButton *confirmButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [confirmButton setTitle:@"确认取消" forState:(UIControlStateNormal)];
    [confirmButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [confirmButton setBackgroundColor:[[UIColor colorWithHexString:@"#70BE68"] colorWithAlphaComponent:1]];
    confirmButton.layer.masksToBounds = YES;
    confirmButton.layer.cornerRadius = 4;
    confirmButton.titleLabel.font = FontSize(14);
    [confirmButton addTarget:self action:@selector(confirmButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [whiteBGView addSubview:confirmButton];
    [confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(36);
        make.left.equalTo(whiteBGView.mas_centerX).offset(13);
        make.right.offset(-28);
        make.bottom.offset(-19);
    }];
}

//取消弹窗 暂不取消按钮 点击事件
-(void)cancelButtonAction:(UIButton *)but{
    [_bgViewsecCancle removeFromSuperview];
    NSLog(@"您点击了 暂不取消 按钮");
    [self.navigationController popToRootViewControllerAnimated:YES];
}

//取消弹窗 确认取消按钮 点击事件
-(void)confirmButtonAction:(UIButton *)but{
    NSLog(@"您点击了 确定取消 按钮");
    [_bgViewsecCancle removeFromSuperview];
}

/**
 弹出框的背景图 取消原因
 */
-(void)shwoBgviewsecConfirm {
    self.bgViewsecConfirm = [[UIView alloc]init];
    self.bgViewsecConfirm.frame = self.view.bounds;
    self.bgViewsecConfirm.backgroundColor = RGBA(1, 1, 1, 0.5);
    UIView *whiteBGView = [[UIView alloc]init];
    whiteBGView.backgroundColor = [UIColor whiteColor];
    whiteBGView.layer.masksToBounds = YES;
    whiteBGView.layer.cornerRadius = 6;
    [self.bgViewsecConfirm addSubview:whiteBGView];
    [whiteBGView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.height.offset(240);
        make.left.offset(22);
        make.right.offset(-22);
    }];
    
    UILabel *UpLable = [[UILabel alloc]initWithFrame:CGRectMake(73, 99, 48, 22)];
    [whiteBGView addSubview:UpLable];
    UpLable.font = FontSize(16);
    UpLable.textColor = [UIColor colorWithHexString:@"#262626"];
    UpLable.textAlignment =  NSTextAlignmentLeft;
    UpLable.text = @"数量:";
    
    UITextField *numberTF = [[UITextField alloc] init];
    //numberTF.backgroundColor = [UIColor orangeColor];
    numberTF.frame = CGRectMake(UpLable.right, 93, 136, 30);
    numberTF.placeholder = @"请输入数量";
    numberTF.textColor = K333333;
    numberTF.font = FontSize(14);
    numberTF.layer.cornerRadius = 5;
    numberTF.layer.borderColor = [UIColor colorWithHexString:@"#BFBFBF"].CGColor;
    numberTF.layer.borderWidth = 1;
    [whiteBGView addSubview:numberTF];
    
    UIButton *cancelButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [cancelButton setTitle:@"取消" forState:(UIControlStateNormal)];
    [cancelButton setTitleColor:[UIColor colorWithHexString:@"#70BE68"] forState:(UIControlStateNormal)];
    [cancelButton setBackgroundColor:[UIColor whiteColor]];
    cancelButton.layer.masksToBounds = YES;
    cancelButton.layer.cornerRadius = 4;
    cancelButton.layer.borderWidth = 1;
    cancelButton.layer.borderColor = [UIColor colorWithHexString:@"#70BE68"].CGColor;
    cancelButton.titleLabel.font = FontSize(14);
    [cancelButton addTarget:self action:@selector(cancelBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [whiteBGView addSubview:cancelButton];
    [cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(36);
        make.left.offset(28);
        make.right.equalTo(whiteBGView.mas_centerX).offset(-13);
        make.bottom.offset(-19);
    }];
    
    UIButton *submitButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [submitButton setTitle:@"提交" forState:(UIControlStateNormal)];
    [submitButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [submitButton setBackgroundColor:[[UIColor colorWithHexString:@"#70BE68"] colorWithAlphaComponent:1]];
    submitButton.layer.masksToBounds = YES;
    submitButton.layer.cornerRadius = 4;
    submitButton.titleLabel.font = FontSize(14);
    [submitButton addTarget:self action:@selector(submitBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [whiteBGView addSubview:submitButton];
    [submitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(36);
        make.left.equalTo(whiteBGView.mas_centerX).offset(13);
        make.right.offset(-28);
        make.bottom.offset(-19);
    }];
}

//取消弹窗 暂不取消按钮 点击事件
-(void)cancelBtnAction:(UIButton *)but{
    [_bgViewsecConfirm removeFromSuperview];
    NSLog(@"您点击了 暂不取消 按钮");
    [self.navigationController popToRootViewControllerAnimated:YES];
}

//取消弹窗 确认取消按钮 点击事件
-(void)submitBtnAction:(UIButton *)but{
    NSLog(@"您点击了 确定取消 按钮");
    [_bgViewsecConfirm removeFromSuperview];
    
}

- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, KHEIGHT)];
        _scrollView.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
        _scrollView.delegate = self;
        _scrollView.contentSize = CGSizeMake(KWIDTH, 100);
        
        adjustInset(_scrollView);
    }
    return _scrollView;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
